package Partition;

import java.util.Random;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-02-05
 * Time:19:59
 * Text：排序数组
 */
public class Text2 {
    public static int[] sortArray(int[] nums) {

        qsort(nums,0,nums.length-1);
        return nums;
    }
    public static void qsort(int[] nums,int l,int r){
        if(l>=r)
            return ;
        int key=nums[new Random().nextInt(r-l+1)+l];
        int left = l-1,right=r+1,i=l;
        while(i<right){
            if(nums[i]<key) swap(nums,++left,i++);
            else if(nums[i]>key) swap(nums,--right,i);
            else i++;
        }
        qsort(nums,l,left);
        qsort(nums,right,r);
    }
    public static void swap(int[] arr,int left,int right){
        int num=arr[left];
        arr[left]=arr[right];
        arr[right]=num;
    }

    public static void main(String[] args) {
        int[] array={5,2,3,1};
        sortArray(array);
    }
}
